<?php
	if(empty($mode)){
		$mode = $_REQUEST['mode'];
	}
	switch($mode){
		case 'upload':
			header('Content-type: application/xml');
			$uploaddir = './uploadedFile/';
			$uploadfile = $uploaddir.basename($_FILES['media']['name']);
			if (move_uploaded_file($_FILES['media']['tmp_name'], $uploadfile)) {
				include "./lib/HttpRequest.php";
				// Create instance
				$h = new HTTPRequest('http://twitpic.com/api/upload');
				// Turn on debug mode (request/response streams will be print)
				//$h->debug(true);
				// Setting 'PATH'
				// Request by GET method
				// Get responsed informations
				// <input type="file" name="image" value="/files/blah.gif"/> and rename to 'myImage.gif'
				$h->attachFile('media', $uploadfile, 'egoing.gif');
				// Request
				$h->send("username={$_POST['username']}&password={$_POST['password']}");
				unlink($uploadfile);
				
				$responseText = $h->responseText;
				$contentType = $h->getResponseHeader('Content-Type');
				echo $responseText;
			} else {
			}
			break;
		case 'spellCheck':
			include "./lib/HttpRequest.php";
			$spellHost = 'http://164.125.36.47/WebSpell_ISAPI.dll?Check';
			$spellBody = $_POST['data'];
			$spellBody = stripslashes($spellBody);
			$spellBody = @iconv('UTF-8', 'EUC-KR', $spellBody);
			$spellBody = preg_replace('/\s\s+/', ' ', trim($spellBody));
		
			$request = new HTTPRequest($spellHost);
			$request->setRequestHeader('contentType', 'application/x-www-form-urlencoded; charset=euc-kr');
			$request->send("&text1=".$spellBody);
			$result = $request->responseText;
			if($result){
				preg_match("@<style.*?</style>@isU", $result, $style);
				$tempStyle = str_replace("td {", ".tdStyle {", $style[0]);
				if(preg_match("@<table border=1.*?</table><br>@isU", $result, $table)){
					$tempTable = preg_replace('@<a href=.*">@isU', '', $table[0]);
					$tempTable = str_replace(array("width=410","width=50","width=220","<td>","e><br>","<td"), array("width=100%","width=100","","<td width=60>","e>","<td class='tdStyle' "), $tempTable);
				}else{
					preg_match("@<font size=2>.*?</font>@isU", $result, $table);
					$tempTable ="<div style='background-color:#ffffff;text-align:center;margin-top:30px;font:9pt dotum;'>".$table[0]."</div>";
				}
				$spellValue = $tempStyle.$tempTable;
			}else{
				$spellValue = "<div style='background-color:#ffffff;text-align:center;margin-top:30px;font:9pt dotum;'>'" . $spellHost . "' Connection it failed.</div>";
			}
			header('Content-Type: text/html; charset=EUC-KR');
			echo "<html><body>$spellValue<div style=\"font-size:11px;text-align:center\" ><a href=\"http://urimal.cs.pusan.ac.kr/urimal_new/\" target=\"_blank\">".@iconv('UTF-8', 'EUC-KR','우리말 배움터에서 제공한 정보입니다')."</a></body></html>";
			break;
		case 'search':
				include "./lib/HttpRequest.php";
				// Create instance
				$h = new HTTPRequest('http://search.twitter.com/search.json?q=monkeyfly&rpp=100');
				$h->send();
				$responseText = $h->responseText;
				$contentType = $h->getResponseHeader('Content-Type');
				echo $responseText;
			break;
	}
	
?>
